package com.aoip.telemetry.telemetry.configureui.business.analysis.addition.service.impl;

import com.aoip.telemetry.telemetry.configureui.business.analysis.addition.mapper.ElementAdditionFiledConfigMapper;
import com.aoip.telemetry.telemetry.configureui.business.analysis.addition.service.AdditionFieldsService;
import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.entity.element.ElementAdditionFiledConfig;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/1/5 9:37
 */
@Service
@DS("telemetry")
public class AdditionFieldsServiceImpl implements AdditionFieldsService {

    @Resource
    private ElementAdditionFiledConfigMapper mapper;
    @Resource
    private RedisCache redisCache;

    @Override
    public List<ElementAdditionFiledConfig> list(ElementAdditionFiledConfig entity) {
        QueryWrapper<ElementAdditionFiledConfig> queryWrapper = new QueryWrapper<>();
        String tableName = entity.getTableName();
        String fieldName = entity.getFieldName();
        String methodDesc = entity.getMethodDesc();
        if (StringUtils.hasText(tableName)) {
            queryWrapper.like(true, "table_name", tableName);
        }
        if (StringUtils.hasText(fieldName)) {
            queryWrapper.like(true, "field_name", fieldName);
        }
        if (StringUtils.hasText(methodDesc)) {
            queryWrapper.like(true, "method_desc", methodDesc);
        }
        return mapper.selectList(queryWrapper);
    }

    @Override
    public ElementAdditionFiledConfig elementAdditionField(String id) {
        return mapper.selectById(id);
    }

    @Override
    public int insert(ElementAdditionFiledConfig entity) {
        redisCache.setCacheObject(CacheKeyEnum.ELEMENT_ADDITION_FILED + ":" + entity.getId(), entity);
        entity.setTableName(entity.getTableName().toLowerCase());
        entity.setFieldName(entity.getFieldName().toLowerCase());
        return mapper.insert(entity);
    }

    @Override
    public int update(ElementAdditionFiledConfig entity) {
        redisCache.setCacheObject(CacheKeyEnum.ELEMENT_ADDITION_FILED + ":" + entity.getId(), entity);
        entity.setTableName(entity.getTableName().toLowerCase());
        entity.setFieldName(entity.getFieldName().toLowerCase());
        return mapper.updateById(entity);
    }

    @Override
    public int initCache() {
        List<ElementAdditionFiledConfig> list = list(new ElementAdditionFiledConfig());
        Collection<String> keys = redisCache.keys(CacheKeyEnum.ELEMENT_ADDITION_FILED + ":*");
        redisCache.deleteObject(keys);
        list.forEach(item -> {
            String tableName = item.getTableName();
            if (StringUtils.hasText(tableName)) {
                item.setTableName(tableName.toLowerCase());
            }
            String fieldName = item.getFieldName();
            if (StringUtils.hasText(fieldName)) {
                item.setFieldName(fieldName.toLowerCase());
            }
            redisCache.setCacheObject(CacheKeyEnum.ELEMENT_ADDITION_FILED + ":" + item.getId(), item);
        });
        return list.size();
    }

    @Override
    public void delete(String[] ids) {
        for (String id : ids) {
            redisCache.deleteObject(CacheKeyEnum.ELEMENT_ADDITION_FILED + ":" + id);
            mapper.deleteById(id);
        }
    }
}
